package com.controller;


import com.bean.Orders;
import com.bean.User;
import com.biz.OrdersBizService;
import com.dto.BaseMsg;
import com.dto.orders.OrderUserDTO;
import com.dto.orders.OrdersBaseInfoDTO;
import com.dto.orders.OrdersUpdateDTO;
import com.exception.InsertException;
import com.exception.UpdateException;
import com.myenum.BizEnumString;
import com.myenum.CodeEnum;
import com.utils.JwtUtil;
import com.utils.PageUtil;
import com.utils.checkutil.OrdersCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author arthorn
 * @create 2022-06-23-0:14
 */

@RestController
@RequestMapping("/orders")
@Slf4j
public class OrderController {
    @Resource
    private OrdersBizService ordersBizService;

    /**
     * 分页模糊查询通过userId根据订单编号、秒杀订单活动名称、订单商品名称、订单状态查询订单
     *
     * @param ordersBaseInfoDTO
     * @return
     */
    @ResponseBody
    @PostMapping("/fuzzyQueryByUserId")
     public BaseMsg queryOrdersByConditionUserId(@RequestBody OrdersBaseInfoDTO ordersBaseInfoDTO,@RequestHeader("Access-Token") String jwt) {
        Long userId = Long.valueOf(JwtUtil.getClaimUserId(jwt, BizEnumString.USER_ID.getMsg()));

        //参数校验
        if (!OrdersCheckUtil.checkOrdersQueryByCondition(ordersBaseInfoDTO)|| ObjectUtils.isEmpty(userId)) {
            BaseMsg failMsg = BaseMsg.fail();
            failMsg.setMsg(CodeEnum.PARAM_CHECK_FAILED.getMsg());
            return failMsg;
        }
        //查询商品
        Map<String, Object> ordersQueryMap = ordersBizService.queryOrdersByConditionUserId(ordersBaseInfoDTO,userId);
        List<OrderUserDTO> ordersList = (List<OrderUserDTO>) ordersQueryMap.get(BizEnumString.ORDERS_LIST.getMsg());
        Integer allOrdersCount = (Integer) ordersQueryMap.get(BizEnumString.ALL_ORDERS_COUNT.getMsg());
        log.info("ordersQueryMap:{}", ordersQueryMap);
        log.info("ordersList:{}", ordersList);
        log.info("allOrdersCount:{}", allOrdersCount);
        //构建分页
        Map<String, Object> pageStartAndEnd = PageUtil.getPageParam(ordersBaseInfoDTO.getBaseInfoDTO(), allOrdersCount, ordersList);
        log.info("pageStartAndEnd:{}", pageStartAndEnd);
        return BaseMsg.succeess().succeessBuildData(pageStartAndEnd);
    }

    /**
     * 根据用户id分页查询所有订单,因为axios不支持json 所以用postMapping
     */
    @ResponseBody
    @PostMapping("/queryAllOrdersByUserId")
    public BaseMsg queryOrders(@RequestBody OrdersBaseInfoDTO ordersBaseInfoDTO,@RequestHeader("Access-Token") String jwt) {
        Long userId = Long.valueOf(JwtUtil.getClaimUserId(jwt, BizEnumString.USER_ID.getMsg()));
        //参数校验
        if (!OrdersCheckUtil.checkOrdersQuery(ordersBaseInfoDTO)|| ObjectUtils.isEmpty(userId)) {
            BaseMsg failMsg = BaseMsg.fail();
            failMsg.setMsg(CodeEnum.PARAM_CHECK_FAILED.getMsg());
            return failMsg;
        }
        //查询商品
        Map<String, Object> ordersQueryMap = ordersBizService.queryOrdersAllByPageByUserId(ordersBaseInfoDTO,userId);
        List<OrderUserDTO> ordersList = (List<OrderUserDTO>) ordersQueryMap.get(BizEnumString.ORDERS_LIST.getMsg());
        Integer allOrdersCount = (Integer) ordersQueryMap.get(BizEnumString.ALL_ORDERS_COUNT.getMsg());
        log.info("ordersQueryMap:{}", ordersQueryMap);
        log.info("ordersList:{}", ordersList);
        log.info("allOrdersCount:{}", allOrdersCount);
        //构建分页
        Map<String, Object> pageStartAndEnd = PageUtil.getPageParam(ordersBaseInfoDTO.getBaseInfoDTO(), allOrdersCount, ordersList);
        log.info("pageStartAndEnd:{}", pageStartAndEnd);
        return BaseMsg.succeess().succeessBuildData(pageStartAndEnd);
    }

    /**
     * 插入订单，传入的ordersPrice单位为分
     * @param ordersBaseInfoDTO
     * @return
     */
    @ResponseBody
    @PostMapping
     public BaseMsg insertOrders(@RequestBody OrdersBaseInfoDTO ordersBaseInfoDTO, @RequestHeader("Access-Token") String jwt) throws InsertException {
        Long adminId = Long.valueOf(JwtUtil.getClaimUserId(jwt, BizEnumString.USER_ID.getMsg()));
        ordersBaseInfoDTO.getOrders().setCreatorId(adminId);
        ordersBaseInfoDTO.getOrders().setUpdatorId(adminId);
        //参数校验
        if (!OrdersCheckUtil.checkOrdersInsert(ordersBaseInfoDTO)) {
            BaseMsg failMsg = BaseMsg.fail();
            failMsg.setMsg(CodeEnum.PARAM_CHECK_FAILED.getMsg());
            return failMsg;
        }
        log.info("ordersBaseInfoDTO:{}", ordersBaseInfoDTO);
        //插入订单
        ordersBizService.insertOrders(ordersBaseInfoDTO);
        return BaseMsg.succeess();
    }

    /**
     * 修改订单-支持修改订单状态（后续可能会增加）
     * @param ordersUpdateDTO
     * @param jwt
     * @return
     * @throws UpdateException
     */
    @ResponseBody
    @PutMapping
    public BaseMsg updateOrders(@RequestBody OrdersUpdateDTO ordersUpdateDTO, @RequestHeader("Access-Token") String jwt) throws UpdateException {
        Long updatorId = Long.valueOf(JwtUtil.getClaimUserId(jwt,BizEnumString.USER_ID.getMsg()));
        //参数校验
        if (!OrdersCheckUtil.checkUpdatorId(updatorId) || !OrdersCheckUtil.checkOrdersUpdate(ordersUpdateDTO)) {
            BaseMsg failMsg = BaseMsg.fail();
            failMsg.setMsg(CodeEnum.PARAM_CHECK_FAILED.getMsg());
            return failMsg;
        }
        log.info("ordersUpdateDTO:{}", ordersUpdateDTO);
        log.info("updatorId:{}", updatorId);
        //修改
        ordersBizService.updateOrders(ordersUpdateDTO,updatorId);
        return BaseMsg.succeess();
    }



}
